package com.putong35.common.customviews;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;

import com.bumptech.glide.Glide;

/**
 * Created by Administrator on 2017/6/25.
 */

public class HorizontalImageLayout extends HorizontalScrollView{
    public LinearLayout mLytContainer;
    private @LayoutRes int imageLayoutRes;
    private  @IdRes
    int imageId;
    public HorizontalImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mLytContainer = new LinearLayout(getContext());
        mLytContainer.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.LEFT));
        addView(mLytContainer);
    }

    public void setImageLayoutRes(@LayoutRes int layoutRes, @IdRes int imageId) {
        this.imageLayoutRes = layoutRes;
        this.imageId = imageId;
    }

    public void addImage(String url, OnClickListener listener) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(imageLayoutRes, null);
        view.setOnClickListener(listener);
        mLytContainer.addView(view);
        ImageView imageView = (ImageView) view.findViewById(imageId);
        Glide.with(getContext()).load(url).into(imageView);
    }

    public void addImage(String url) {
        addImage(url, null);
    }

    public void clearImageViews() {
        mLytContainer.removeAllViews();
    }


}
